package com.stefanini.pe.bbva.bp.in.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ComponentSystemEvent;
import javax.faces.model.SelectItem;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import com.stefanini.pe.bbva.bp.in.bo.iface.ResumenGeneralBOIface;
import com.stefanini.pe.bbva.bp.in.model.ComboModel;
import com.stefanini.pe.bbva.bp.in.model.LoginModel;
import com.stefanini.pe.bbva.bp.in.model.ResumenGeneralModel;
import com.stefanini.pe.bbva.bp.in.security.AuthorizationService;
import com.stefanini.pe.bbva.bp.in.to.ResumenGeneral;
import com.stefanini.pe.bbva.bp.in.to.UsuarioSesion;
import com.stefanini.pe.bbva.bp.in.utils.ConstantesWeb;
import com.stefanini.pe.bbva.bp.in.utils.SpringApplicationContext;
import com.stefanini.pe.bbva.bp.in.utils.Util;

@Controller("resumenGeneralController")
@RequestScoped
public class ResumenGeneralController extends GenericoController {

	private static Logger logger = Logger.getLogger(ResumenGeneralController.class);
	
	@Autowired
	private ResumenGeneralBOIface resumenGeneralBO;

	private LoginModel loginModel;
	@Autowired
	private ResumenGeneralModel resumenGeneralModel; 
	
	public void onPageLoad(ComponentSystemEvent event){
		try{
			AuthorizationService auth = (AuthorizationService)SpringApplicationContext.getBean("authorizationService");
			if (auth.isSessionUserAndContextValide() && !FacesContext.getCurrentInstance().isPostback()) {
				System.out.println("ResumenGeneralController... is not Postback");
				
				LoginController loginContr = (LoginController)getHttpSession().getAttribute("loginController");
				loginModel = loginContr.getLoginModel();
				
				this.resumenGeneralModel.setListaConsultaGeneral(new ArrayList<ResumenGeneral>());
				this.resumenGeneralModel.setCodigoAsesor(((UsuarioSesion) loginModel.getUsuarioSesion()).getIdUsuarioAsesor().toString());
				this.loginModel.setMostrarListaEjecutivos(true);
				resumenGeneralModel.setCodigoAnio("");
				this.resumenGeneralModel.setMostrarAsesores(this.loginModel.getMostrarListaEjecutivos());
				getSessionMap().put(ConstantesWeb.Reportes.PARAM_NOMBRE_REPORTE,ConstantesWeb.Reportes.REPORTE_RESUMEN_GENERAL_ASESORES);
				getSessionMap().put("mapParam",new HashMap<String,Object>());
				
				ComboModel comboModel = (ComboModel)SpringApplicationContext.getBean("comboModel");
				List<SelectItem> listAnio = comboModel.getListaAnio();
				if(listAnio != null && listAnio.size()>0){
					SelectItem iAnio = listAnio.get(0);
					resumenGeneralModel.setCodigoAnio(Util.getStringF(iAnio.getValue()));
				}
				
				buscar();
			}
		}catch(Exception e){
			e.printStackTrace();
			logger.error(e.getMessage(),e);
			mostrarError("Error interno:", e.getMessage());
		}
	}	
	
	public void buscar(){
		try{
			if (!resumenGeneralModel.getCodigoAnio().equals("")) {
				List<ResumenGeneral> listResumenGeneral = resumenGeneralBO.consultaResumen(this.resumenGeneralModel.getCodigoAnio());
				if(listResumenGeneral.size()>0){
					for(int i=0;i<listResumenGeneral.size();i++){
						listResumenGeneral.get(i).setTotalCliente(Util.nvlNumber(listResumenGeneral.get(i).getNroclcompartidos()+listResumenGeneral.get(i).getNroclpropios()).toString());
						listResumenGeneral.get(i).setTotalPerfilados(Util.nvlNumber(listResumenGeneral.get(i).getNroclcompartidosperfilados()+listResumenGeneral.get(i).getNroclpropiosperfilados()).toString());
						listResumenGeneral.get(i).setTotalRebalanceados(Util.nvlNumber(listResumenGeneral.get(i).getNroclcompartidosrebalanceados()+listResumenGeneral.get(i).getNroclpropiosrebalanceados()).toString());
						listResumenGeneral.get(i).setTotalSaldo((listResumenGeneral.get(i).getNroclpropiossaldos().add(listResumenGeneral.get(i).getNroclcompartidossaldos())));
					}
				}
				resumenGeneralModel.setListaConsultaGeneral(listResumenGeneral);
				getParametrosReporte().put("listResumenGeneral", listResumenGeneral);
				getSessionMap().put("mapParam", getParametrosReporte());
			}
		}catch(Exception e){
			e.printStackTrace();
			logger.error(e.getMessage(),e);
			mostrarError("Error interno:", e.getMessage());
		}	
	}
	
	public ResumenGeneralModel getResumenGeneralModel() {
		return resumenGeneralModel;
	}
}
